package com.koron.sinoprobe.web.engineering.controller;


import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.WorkbookUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.EasyExcelUtils;
import com.koron.sinoprobe.web.engineering.bean.po.SpDivisionProject;
import com.koron.sinoprobe.web.engineering.bean.vo.SpDivisionProjectVO;
import com.koron.sinoprobe.web.engineering.excel.ExcelError;
import com.koron.sinoprobe.web.engineering.excel.SpDivisionProjectListener;
import com.koron.sinoprobe.web.engineering.excel.SpDivisionProjectSheetWriteHandler;
import com.koron.sinoprobe.web.engineering.service.SpDivisionProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 项目划分工程(SpDivisionProject)表控制层
 *
 * @author jzp
 * @since 2021-12-07 17:42:35
 */
@Api(tags = "项目实施-项目划分-划分工程接口")
@RestController
@RequestMapping("sp/DivisionProject")
public class SpDivisionProjectController {
    /**
     * 服务对象
     */
    @Autowired
    private SpDivisionProjectService spDivisionProjectService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Response<PageInfo<SpDivisionProject>> page(PageQuery pageQuery, SpDivisionProject spDivisionProject) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), spDivisionProjectService, "page", PageInfo.class, pageQuery, spDivisionProject));
    }

    @SneakyThrows
    @ApiOperation(value = "下载模板")
    @GetMapping("template")
    public void template(HttpServletResponse response) {
        String path = "excel/division.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        Workbook book = WorkbookUtil.createBook(classPathResource.getInputStream());
        ExcelWriter writer = new ExcelWriter(book, "项目划分");
        String fileName = "division";

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+ fileName +".xlsx");

        ServletOutputStream out=response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }

    @SneakyThrows
    @ApiOperation(value = "导入")
    @PostMapping("import")
    public Response importFile(@RequestParam("file") MultipartFile file) {
        String filename = CodeTools.getCodeNo("SpDivisionProject", 3);
        String tempPath = EasyExcelUtils.createTempFile(filename, "xlsx");

        List<SpDivisionProjectVO> lists = new ArrayList<>();

        List<Map<Integer, String>> listMap = new ArrayList<>();
        EasyExcel.read(file.getInputStream(), new SpDivisionProjectListener(listMap))
                .extraRead(CellExtraTypeEnum.MERGE)
                .sheet("项目划分").doRead();
        ExcelError excelError = spDivisionProjectService.readExcel(listMap, lists);

        if (excelError == null) {
            return Response.ok(lists);
        }
        EasyExcel.write(tempPath)
                .inMemory(true)
                .registerWriteHandler(new SpDivisionProjectSheetWriteHandler(excelError))
                .withTemplate(file.getInputStream())
                .sheet("项目划分").doWrite(new ArrayList<>());
        return Response.ok("temp/" + filename + ".xlsx");
    }

}
